package index;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

@WebServlet("/DeleteDishServlet")
public class DeleteDishServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 检查管理员权限
        HttpSession session = request.getSession(false);
        if (session == null || !"admin".equals(session.getAttribute("role"))) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN, "权限不足");
            return;
        }

        int dishId = Integer.parseInt(request.getParameter("dishId"));

        if (JdbcUtils.deleteDish(dishId)) {
            response.sendRedirect("AdminDishListServlet?status=delete_success");
        } else {
            response.sendRedirect("error.jsp?msg=Delete failed");
        }
    }
}
